package net.xdclass.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.request.CartItemRequest;
import net.xdclass.service.CartService;
import net.xdclass.util.JsonData;
import net.xdclass.vo.CartItemVO;
import net.xdclass.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "购物车")
@RestController
@Slf4j
@RequestMapping("/api/cart/v1")
public class CartController {
    @Autowired
    private CartService cartService;

    @ApiOperation("添加到购物车")
    @PostMapping("add")
    public JsonData addToCart(@RequestBody CartItemRequest cartItemRequest){
        cartService.cartAdd(cartItemRequest);
        return JsonData.buildSuccess();
    }

    @ApiOperation("清空购物车")
    @DeleteMapping("delete")
    public JsonData delete(){
        cartService.delete();
        return JsonData.buildSuccess();
    }

    @ApiOperation("查看购物车")
    @GetMapping("queryCart")
    public JsonData queryCart(){
        CartVO cartVO = cartService.queryCart();
        return JsonData.buildSuccess(cartVO);
    }

    @ApiOperation("删除购物车某一项")
    @DeleteMapping("delete/{product_id}")
    public JsonData deleteItem(@PathVariable("product_id") long productId){
        cartService.deleteItem(productId);
        return JsonData.buildSuccess();
    }

    @ApiOperation("修改购物车商品数量")
    @PostMapping("change")
    public JsonData change(@RequestBody CartItemRequest cartItemRequest){
        cartService.change(cartItemRequest);
        return JsonData.buildSuccess();
    }

}
